<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>仓库列表</title>
  <script type="text/javascript">
      var newWarehouseWin;
      var editWarehouseWin;
      var warehouseDS;

      //处理供应商锁定
      function lockWarehouseFn(id) {
          Ext.Ajax.request({
              url: '/warehouse/warehouseLock',
              success: function (result, request) {
                  warehouseDS.load();
              },
              failure: function (result, request) {
                  Ext.MessageBox.show({
                      title: '消息',
                      msg: "通讯失败，请从新操作",
                      buttons: Ext.MessageBox.OK,
                      icon: Ext.MessageBox.WARNING
                  });
              },
              method: 'GET',
              params: {
                  id: id
              }
          });
      }

      Ext.onReady(function () {
          //------------------------showWarehouseSetupWin---------------------------
          //准备显示仓库设置的window

          //生成有关仓库的Grid
          warehouseDS = Ext.create('Ext.data.Store', {
              autoDestroy: true,
              autoSync: true,
              model: 'Warehouse',
              proxy: {
                  type: 'ajax',
                  url: '/warehouse/getAllWarehouse',
                  reader: {
                      type: 'json',
                      root: 'root',
                      totalProperty: 'totalProperty'// 数据集记录总数
                  }
              },
              pageSize: pageSize + 15,//default:25角色
              autoLoad: true
          });

          //锁定物流公司
          function renderWarehouseIsLucked(value, cellmeta, record, index, columnIndex, store) {
              if (record.get("is_locked") == "1") {
                  return "<a style=cursor:pointer onclick=lockWarehouseFn(" + store.getAt(index).get('id') + ")><em style=color:red>锁定</em></a>";
              } else {
                  return "<a style=cursor:pointer onclick=lockWarehouseFn(" + store.getAt(index).get('id') + ")><em style=color:green>未锁定</em></a>";
              }
          }

          var warehouseGrid = Ext.create('Ext.grid.Panel', {
              width: screenWidth - 165,
              height: screenHeight - 285,
              loadMask: true,
              store: warehouseDS,
              columns: [Ext.create('Ext.grid.RowNumberer'), {
                  header: 'id',
                  dataIndex: 'id',
                  width: 50
              }, {
                  header: '所属公司',
                  dataIndex: 'company_name',
                  width: 100
              }, {
                  header: '编号', dataIndex: 'number', width: 80
              }, {
                  header: '仓库名称',
                  dataIndex: 'name',
                  width: 150
              }, {
                  header: '类型',
                  dataIndex: 'warehouse_type_name',
                  width: 150
              }, {
                  header: '描述',
                  dataIndex: 'description',
                  width: 400
              }, {
                  header: '是否锁定',
                  dataIndex: 'is_locked',
                  renderer: renderWarehouseIsLucked,
                  width: 60
              }, {
                  header: '生成日期',
                  dataIndex: 'created_at',
                  renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'), width: 115
              }, {
                  header: '修改日期',
                  dataIndex: 'updated_at',
                  renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'), width: 115
              }],
              selModel: Ext.create('Ext.selection.CheckboxModel'),
              iconCls: 'icon-grid',
              //collapsible: true,
              //animCollapse: false,
              frame: true,
              bbar: Ext.create('Ext.toolbar.Paging', {
                  pageSize: 25,
                  store: warehouseDS,
                  displayInfo: true,
                  displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
                  emptyMsg: "没有记录"
              }),
              tbar: [
                  {
                      text: '添加',
                      iconCls: 'addItem',
                      handler: function () {
                          newWarehouseWin = Ext.create('Ext.window.Window', {
                              layout: 'fit',
                              width: 300,
                              height: 250,
                              closeAction: 'destroy',
                              plain: true,
                              modal: true,
                              constrain: true,
                              //modal: true,
                              title: '添加仓库',
                              autoLoad: {
                                  url: "/warehouse/newWarehouse",
                                  scripts: true
                              }
                          });
                          newWarehouseWin.on("destroy", function () {
                              warehouseDS.load();
                          });
                          newWarehouseWin.resizable = false;
                          newWarehouseWin.show();
                      }
                  },
                  '-',
                  {
                      text: '编辑',
                      iconCls: 'editItem',
                      handler: function () {
                          var records = warehouseGrid.getSelectionModel().getSelection();
                          if (records.length == 0) {
                              Ext.MessageBox.show({
                                  title: '提示信息',
                                  msg: "请先选中一条记录后，再编辑。",
                                  buttons: Ext.MessageBox.OK,
                                  icon: Ext.MessageBox.WARNING
                              });
                          } else {
                              //把表单添加到窗口中
                              editWarehouseWin = Ext.create('Ext.window.Window', {
                                  title: '编辑仓库',
                                  layout: 'fit',
                                  width: 300,
                                  height: 250,
                                  closeAction: 'destroy',
                                  constrain: true,
                                  plain: true,
                                  modal: true,
                                  autoLoad: {
                                      url: "/warehouse/editWarehouse?id=" + records[0].get('id'),
                                      scripts: true
                                  }
                              });
                          }
                          editWarehouseWin.on("destroy", function () {
                              warehouseDS.load();
                          });
                          editWarehouseWin.resizable = false;
                          editWarehouseWin.show();
                      }
                  }
              ]
          });
          warehouseGrid.render('warehouse_list');
      })
  </script>
</head>
<body>
<div id="warehouse_list"></div>
</body>
</html>
